package com.hmys.api.controller;

import com.github.pagehelper.PageInfo;
import com.hmys.api.model.WebNews;
import com.hmys.api.model.WebNewsType;
import com.hmys.api.response.Result;
import com.hmys.api.response.ResultCode;
import com.hmys.api.response.ResultPage;
import com.hmys.api.service.WebNewsService;
import com.hmys.api.service.WebNewsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * Created by chen365 on 2018/03/16.
 */
@RestController
@Api(tags = "新闻")
@RequestMapping(value = "/news")
public class NewsController {

    @Autowired
    private WebNewsService webNewsService;

    @Autowired
    private WebNewsTypeService webNewsTypeService;

    @ApiOperation(value = "列表", notes = "列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "type", value = "新闻类型", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageIndex", value = "页码", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页数", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "Long", name = "cityId", value = "城市ID", required = true, paramType = "query")
    })
    @GetMapping(value = "/list/{type}/{pageIndex}/{pageSize}")
    public Object list(@PathVariable("type") Long type,
                       @PathVariable("pageIndex") Integer pageIndex,
                       @PathVariable("pageSize") Integer pageSize, @PathParam("cityId") Long cityId) {
        WebNews record = new WebNews();
        if (!StringUtils.isEmpty(type) && type > 0) {
            record.setType(type);
        }
        if (!StringUtils.isEmpty(cityId) && cityId > 0) {
            record.setCityId(cityId);
        }
        record.setStatus(true);
        PageInfo<WebNews> pageInfo = webNewsService.selectByBean(pageIndex, pageSize, record);
        if (pageInfo.getList().size() > 0) {
            return new ResultPage(ResultCode.SUCCESS, pageInfo);
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "详情", notes = "详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "新闻ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/detail/{id}")
    public Object detail(@PathVariable("id") Long id) {
        if (!StringUtils.isEmpty(id) && id < 1) {
            return new Result<>(ResultCode.ERROR_PARAM);
        }
        WebNews record = webNewsService.selectByPrimaryKey(id);
        if (record != null) {
            if (record.getStatus()) {
                return new Result<>(ResultCode.SUCCESS, record);
            } else {
                return new Result<>(ResultCode.HAVE_DELETED);
            }
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "新闻类型列表", notes = "新闻类型列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "pid", value = "父级ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/type/list/{pid}")
    public Object typeList(@PathVariable("pid") Long pid) {
        if (!StringUtils.isEmpty(pid) && pid < 0) {
            return new Result<>(ResultCode.ERROR_PARAM);
        }
        WebNewsType record = new WebNewsType();
        record.setStatus(true);
        record.setPid(pid);
        List<WebNewsType> list = webNewsTypeService.selectByBean(record);
        if (list.size() > 0) {
            return new Result<>(ResultCode.SUCCESS, list);
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "新闻类型详情", notes = "新闻类型详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "类型ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/type/detail/{id}")
    public Object typeDetail(@PathVariable("id") Long id) {
        if (!StringUtils.isEmpty(id) && id < 1) {
            return new Result<>(ResultCode.ERROR_PARAM);
        }
        WebNewsType record = webNewsTypeService.selectByPrimaryKey(id);
        if (record != null) {
            if (record.getStatus()) {
                return new Result<>(ResultCode.SUCCESS, record);
            } else {
                return new Result<>(ResultCode.HAVE_DELETED);
            }
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }
}
